package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> listAll() {
        return roleMapper.listAll();
    }

    @Override
    public void insert(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void save(Role role, Long[] ids) {
        roleMapper.insert(role);
        for (int i=0;i< ids.length;i++)
        {
            roleMapper.insertRelationRoleIdAndPermissionId(role.getId(), ids[i]);
        }
    }

    @Override
    public List<Permission> listAllPermission(Long id) {
        return roleMapper.selectPermissionByRoleId(id);
    }

    @Override
    public Role selectById(Long id) {
        return roleMapper.seletById(id);
    }

    @Override
    public void updateById(Role role,Long[] ids) {
        roleMapper.updateById(role);

        roleMapper.deleteRelationById(role);
        for (int i=0;i< ids.length;i++)
        {
            roleMapper.insertRelationRoleIdAndPermissionId(role.getId(), ids[i]);
        }
    }

    @Override
    public List<Permission> selectPermissionByRoleId(Long id) {
        return roleMapper.selectPermissionByRoleId(id);
    }


}
